// pages/userCourse/list.js
import {
  request,
  toPage
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadmore:'loading',
    page:1,
    pagesize:10,
    isRequest:false
  },
  toPage: toPage,
  queryCourseList(reload = false) {
    let {isRequest,page,pagesize,list}=this.data;
    if(!isRequest){
      this.data.isRequest=true
      let loadmore=this.data.loadmore!='nomore'?'nomore':'loading'
      this.setData({loadmore})
      request({
        url: 'User/userBookList',
        method: 'POST',
        data: {
          page,
          pagesize
        },
        success: res => {
          this.data.isRequest=false
          // console.log(res)
          let requestList = res.list;
          if(requestList.length>0){
            this.data.page++
            loadmore=requestList.length==pagesize?'getmore':'nomore';
          }else{
            loadmore='nomore';
          }
          if(page>1){
            res.list = [...list,...res.list];
          }
          this.setData(res)
          this.setData({loadmore})
          wx.stopPullDownRefresh()
        },
        fail:err=>{
          this.data.isRequest=false
          this.setData({loadmore:'error'})
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryCourseList(true)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  //上一个页面
    let {class_hour,class_hour_surplus,c_teacher,f_teacher,c_teacher_surplus,f_teacher_surplus}=prevPage.data.user
    this.setData({class_hour,class_hour_surplus,c_teacher,f_teacher,c_teacher_surplus,f_teacher_surplus})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryCourseList(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.queryCourseList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})